package cn.finder.httpcommons.request;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

import cn.finder.httpcommons.EnumObject;
import cn.finder.httpcommons.attri.JsonIgnore;
import cn.finder.httpcommons.attri.JsonProperty;
import cn.finder.httpcommons.response.ApiResponse;
import cn.finder.httpcommons.utils.DateUtils;

public abstract class SearchRequest<T extends ApiResponse> extends
		DefaultRequest<T> {

	public final static int PAGESIZE_MAX=999999999;
	public final static int PAGESIZE_DEFAULT=10;
	
	private int pageIndex = 1;

	private int pageSize = 10;

	// 排序字段 目前只支持一个字段的排序
	private String sortField;

	// 排序方式
	private String sortDirect;

	// 返回的数据不包含的字段(多个字段之间使用 逗号 隔开)
	private String excludeProperties;

	// 返回的数据包含的字段(多个字段之间使用 逗号 隔开)
	private String includeProperties;

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getSortField() {
		return sortField;
	}

	public void setSortField(String sortField) {
		this.sortField = sortField;
	}

	public String getSortDirect() {
		return sortDirect;
	}

	public void setSortDirect(String sortDirect) {
		this.sortDirect = sortDirect;
	}

	@JsonProperty(name = "excludeProperties")
	public String getExcludeProperties() {
		return excludeProperties;
	}

	public void setExcludeProperties(String excludeProperties) {
		this.excludeProperties = excludeProperties;
	}

	@JsonProperty(name = "includeProperties")
	public String getIncludeProperties() {
		return includeProperties;
	}

	public void setIncludeProperties(String includeProperties) {
		this.includeProperties = includeProperties;
	}

	/***
	 * 默认 获取请求参数 如果参数值为null 或者字符串为 空，那么不进行提交
	 */
	@Override
	public Map<String, String> parameters() {

		Class<? extends DefaultRequest> c = this.getClass();

		Method[] methods = c.getMethods();
		if (methods != null && methods.length > 0) {

			for (Method method : methods) {

				String methodName = method.getName();

				String propName = "";

				if (methodName.startsWith("get") && !methodName.equals("getClass")) {
					JsonIgnore jsonIgnore = method
							.getAnnotation(JsonIgnore.class);
					if (jsonIgnore != null) {
						continue;
					}

					JsonProperty jsonProperty = method
							.getAnnotation(JsonProperty.class);
					if (jsonProperty == null) {

						String nametmp = methodName.substring(3);
						propName = nametmp.substring(0, 1).toLowerCase()
								+ nametmp.substring(1);
					} else {
						if (jsonProperty.name() != null
								&& jsonProperty.name().trim().length() > 0) {
							propName = jsonProperty.name();

						}
					}

					Object propertyValue = null;

					try {
						propertyValue = method.invoke(this);
					} catch (Exception e) {
						e.printStackTrace();
					}

					if (propertyValue instanceof EnumObject<?>) {
						propertyValue = ((EnumObject<?>) propertyValue)
								.toString();

					}

					if (propertyValue != null && !propertyValue.equals("")) {// 如果属性值
																				// 为
																				// null
																				// 或者空
																				// ""，
																				// 不会发送此参数
						if (propertyValue.toString().trim().length() > 0) {
							if (propertyValue instanceof Date) {
								Date pv = (Date) propertyValue;

								propertyValue = DateUtils.formatDate(pv);

							}

							addParameter(propName, propertyValue.toString());
						}

					}
				}
			}

		}
		return parameters;
	}
}